﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Mail;
using PenoyShop.Utilities;
using System.Configuration;
using System.IO;
using PeonyShop.Bussiness;
using System.Data;
using System.Web.Services;

namespace PeonyShop
{
    public partial class ContactUs : System.Web.UI.Page
    {
        public string sImgName;
        protected void Page_Load(object sender, EventArgs e)
        {
            BImage bImg = new BImage();

            //DataSet ds = bImg.GetImage(string.Empty,
        }

       

        protected void btnSendMess_Click(object sender, EventArgs e)
        {

            #region Read file template
            string sFileName = Server.MapPath(@"~/HTML/MailContact.html");
            StreamReader sR = File.OpenText(sFileName);
            string sTemplate = sR.ReadToEnd();
            #endregion

            string sTitle = string.Empty;
            if(radTitleMr.Checked)
              sTitle = "Mr";
            if(radTitleMrs.Checked)
                sTitle  ="Mrs";

            string sName = txtName.Text.Trim();
            string sEmail = txtEmail.Text.Trim();
            string sPhone = txtPhone.Text.Trim();
            string sSubject = txtSucject.Text.Trim();
            string sMessage = txtMessage.Text.Trim();

            
            string sToEmail = ConfigurationManager.AppSettings["ToEmail"].ToString();

            if(!string.IsNullOrEmpty(sTitle))
                sTemplate = sTemplate.Replace("[TITLE]",sTitle);
            else
                sTemplate = sTemplate.Replace("[TITLE]", "Không cung cấp chức danh");
            if (!string.IsNullOrEmpty(sName))
                sTemplate = sTemplate.Replace("[NAME]", sName);
            else
                sTemplate = sTemplate.Replace("[NAME]", "Không cung cấp tên");
            if (!string.IsNullOrEmpty(sEmail))
                sTemplate = sTemplate.Replace("[EMAIL]", sEmail);
            else
                sTemplate = sTemplate.Replace("[EMAIL]", "Không cung cấp email");
            if (!string.IsNullOrEmpty(sPhone))
                sTemplate = sTemplate.Replace("[PHONE]", sPhone);
            else
                sTemplate = sTemplate.Replace("[PHONE]", "Không cung cấp số điện thoại");
            if (!string.IsNullOrEmpty(sSubject))
                sTemplate = sTemplate.Replace("[SUBJECT]", sSubject);
            else
                sTemplate = sTemplate.Replace("[SUBJECT]", "Không có subject");
            if (!string.IsNullOrEmpty(sMessage))
                sTemplate = sTemplate.Replace("[MESSAGE]", sMessage);
            else
                sTemplate = sTemplate.Replace("[MESSAGE]", "Không có nội dung");
            

            try
            {
                string sResult = Sendmail(sToEmail, sTemplate);
                if (string.Equals(sResult,""))
                    spResult.Text = "Thank you for  sending your request. We'll reply you as soon as posible.";
                else
                    spResult.Text = "Error system, can not send this message. Please send by your email account (Yahoo or Gmail ...)";
            }
            catch (Exception ex)
            {
                Logger.FrameworkLogger.Error("send mail ", ex);
                spResult.Text = "Error system, can not send this message. Please send by your email account (Yahoo or Gmail ...)";
            }

         
        }

        public string Sendmail(string sToEmail, string Body)
        {
            try
            {
                string sFromEmail = "popy@gesoft69.com";
                string Password = "tam123456";
                string Title = "Contact from client";
                string DisplayName = "Client";
                int port = 587;
                string smtp = "smtp.gmail.com";


                using (MailMessage mail = new MailMessage())
                {
                    mail.From = new MailAddress(sFromEmail, DisplayName);
                    mail.To.Add(sToEmail);
                    mail.CC.Add(ConfigurationManager.AppSettings["CCToEmail"].ToString());
                    mail.Bcc.Add(ConfigurationManager.AppSettings["BBToEmail"].ToString());
                    mail.Subject = Title;
                    mail.Body = Body;
                    mail.IsBodyHtml = true;
                    SmtpClient client = new SmtpClient(smtp, port);
                    client.Credentials = new System.Net.NetworkCredential(sFromEmail, Password);
                    client.EnableSsl = true;
                    client.Send(mail);
                    return "";
                }

                //using (MailMessage mail = new MailMessage())
                //{
                //    mail.From = new MailAddress(Email, DisplayName);
                //    mail.To.Add(To);
                //    mail.Subject = Title;
                //    mail.Body = Body;
                //    mail.IsBodyHtml = true;
                //    SmtpClient client = new SmtpClient(smtp, CodeHelper.GetIntValue(port));
                //    client.Credentials = new System.Net.NetworkCredential(Email, Password);
                //    client.EnableSsl = true;
                //    client.Send(mail);
                //}

            }
            catch (Exception ex)
            {
                Logger.FrameworkLogger.Error("Sendmail", ex);
                return ex.Message.ToString();
            }
        }

    }
}